package com.perform.utils.sm2;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;

/**
 * 国密算法加解密示例（用于java后端加解密）
 *
 * @author c-fanqj
 * @date 2022/9/29
 * @apiNote
 */
public class SmDemo {

    private static final byte[] SM3_SALT = "AABBCC112233".getBytes();

    public static void main(String[] args) {
        // 创建 SM2 公私钥对
        //createSM2Key();

        String publicKey, privateKey = "";
        //publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE5VvMPGmFL8nkIjpPoYM3yLIvSu9PICPHde1tyulQEcibWd3/G1FcQ2VGQ234TAk77ylUULia64cawfHhif6TXQ==";
        //privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgXvCy40dLdzlGHkRSGPewU2xVMOfEIS30QkjNlhizZAugCgYIKoEcz1UBgi2hRANCAATlW8w8aYUvyeQiOk+hgzfIsi9K708gI8d17W3K6VARyJtZ3f8bUVxDZUZDbfhMCTvvKVRQuJrrhxrB8eGJ/pNd";

        publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE2QqvmMx7SdrtKcM5+6i6BFym+jSBwYU1CfYHIKsadMcoBWef/SUtHvx1M/svhzVQtNZimCr71+rFaY/SuPFoPg==";
        privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgBDvYyvKgmgcno89z0htT6/72YeUKJbuEJJc5S9kKOVCgCgYIKoEcz1UBgi2hRANCAATZCq+YzHtJ2u0pwzn7qLoEXKb6NIHBhTUJ9gcgqxp0xygFZ5/9JS0e/HUz+y+HNVC01mKYKvvX6sVpj9K48Wg+";

        String plainText = "abc123";

        // SM2 公钥加密
        String ciphertext = "";
        //ciphertext = sm2Encrypt(publicKey, plainText);
        /*for (int i = 0; i < 3; i++) {
            ciphertext = sm2Encrypt(publicKey, plainText);
            System.out.println("ciphertext " + i + " " + ciphertext);
        }*/

        ciphertext = "04c884051dfd96bd0720113c126318c4324d3c392c5a864b1d44443082ba25e19d047a73a5a6ddd85b628df78cf15c2a37224c79af357a4566a663218ebdbcc4e0e725a783abbad71e0a4bd8db56786da78e17dee434a0fe78383a2b6318b567c98198d6834df9";
        // SM2 私钥解密
        sm2Decrypt(privateKey, ciphertext);

        // SM3 取摘要
        //SM3 sm3 = new SM3(SM3_SALT);
        //System.out.println(sm3.digestHex(plainText));
    }

    /**
     * 创建 SM2 公私钥对
     */
    private static void createSM2Key() {
        SM2 sm2 = new SM2();
        System.out.println("公钥：" + sm2.getPublicKeyBase64());
        System.out.println("私钥：" + sm2.getPrivateKeyBase64());
    }

    /**
     * SM2 公钥加密
     *
     * @param publicKey
     * @param plainText
     */

    public static String sm2Encrypt(String publicKey, String plainText) {
        System.out.println("SM2 公钥加密");
        System.out.println("明文：" + plainText);
        SM2 sm2 = new SM2(null, publicKey);
        String ciphertext = sm2.encryptBase64(plainText, KeyType.PublicKey);
        System.out.println("密文：" + ciphertext);
        return ciphertext;
    }

    /**
     * SM2 私钥解密
     *
     * @param privateKey
     * @param ciphertext
     */
    public static String sm2Decrypt(String privateKey, String ciphertext) {
        System.out.println("SM2 私钥解密");
        System.out.println("密文：" + ciphertext);
        SM2 sm2 = new SM2(privateKey, null);
        String plainText = sm2.decryptStr(ciphertext, KeyType.PrivateKey);
        System.out.println("明文：" + plainText);
        return plainText;
    }

}
